home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- Copyright (c) 2008 Ensolis, LLC. All Rights Reserved.
- ----------------------------------------------------------------------------*/
-
- /******************************************************************************
- * Interfaces used by a service to convert parsed weather data from one
- * unit of measure to another. Supplies a set
- * items and methods used for manipulating the data.
- *
- * @status FROZEN
- * @version 1.0
- ******************************************************************************/
- function ConverterService()
- {
- //setup a new error
- this._error = Cc["@ensolis.com/forecastfox/error-item;1"].
- createInstance(Ci.ffIErrorItem);
- }
-
- ConverterService.prototype = {
- __proto__: new ServiceBase("ConverterService"),
-
- ////////////////////////////////
- // ffIService
-
- /**
- * Initialize the component. Called by the manager service.
- */
- start: function ConverterService_start()
- {
- //setup the variables
- this._items = {};
-
- //load the converters
- return this._loadConverters();
- },
-
- /**
- * Destroy the component. Called by the manager service. This may be
- * called prior to start so it needs to be safe.
- */
- stop: function ConverterService_stop()
- {
- //clear variables
- this._items = null;
- },
-
- ////////////////////////////////
- // ffIConverterService
-
- /**
- * Service has a specific item.
- *
- * @param ID of the converter item.
- * @return True if item exists.
- */
- hasItem: function ConverterService_hasItem(aID)
- {
- return this._items.hasOwnProperty(aID);
- },
-
- /**
- * Retrieve a converter item returns null if item doesn't exist.
- *
- * @param ID of the converter item.
- * @return A ffIConverterItem.
- */
- getItem: function ConverterService_getItem(aID)
- {
- if (!this.hasItem(aID))
- return null;
- else
- return this._createItem(this._items[aID]);
- },
-
- /**
- * Retrieve an array of converter items.
- *
- * @param Conversion of the item.
- * @param Count of items in the array.
- * @return An array of ffIConverterItem components.
- */
- getItems: function ConverterService_getItems(aConversion, aCount)
- {
- //get the current uom
- var units = getPref("units.current");
-
- //loop through items
- var items = [];
- for (var id in this._items) {
- var item = this._items[id];
-
- //match on conversion and units
- //exclude the default conversion
- if (item.conversion == aConversion &&
- item.units == units && item.name != "none")
- items.push(this.getItem(id));
- }
-
- //return the array
- aCount.value = items.length;
- return items;
- },
-
- /**
- * Format the value of a parser item.
- *
- * @param Conversion name.
- * @param Name of the converter or null.
- * @param Feed data to format.
- * @return The formatted value of the itme.
- */
- formatValue: function ConverterService_formatValue(aConversion, aConverter, aValue)
- {
- //get the current uom
- var units = String(getPref("units.current"));
-
- //get the converter name
- var converter = (!aConverter) ? "none" : aConverter;
-
- //use the default if the requested converter not found
- var id = aConversion + "-" + units + "-" + converter;
- if (!this.hasItem(id))
- id = aConversion + "-" + units + "-none";
-
- //replace the value in the calculation
- converter = this._items[id];
- var calc = converter.calc;
- calc = calc.replace(/\$VAL/g, "'" + String(aValue) + "'");
-
- //replace the calcualation in the format string
- var format = converter.format;
- format = format.replace(/\$CALC/g, calc);
-
- //translate the abbreviation if we have it
- var abbr = "''";
- if (converter.hasOwnProperty("abbr")) {
- abbr = converter.abbr;
- try {
- abbr = this.bundle.GetStringFromName("ff.converters.abbr." +
- converter.abbr);
- } catch(e) {}
- }
-
- //replace the abbreviation in the format string
- format = format.replace(/\$ABBR/g, abbr);
-
- //return the formatted string
- try {
- return eval(format);
- } catch(e) {}
-
- //return the unformatted value because the eval failed
- return aValue;
- },
-
- ////////////////////////////////
- // Internal Functions
-
- /**
- * Create a converter item from a javascript object
- * representing that item.
- *
- * @param The javascript representation of the item.
- * @return The converter item.
- */
- _createItem: function ConverterService__createItem(aItem)
- {
- //create an empty converter item
- var item = Cc["@ensolis.com/forecastfox/converter-item;1"].
- createInstance(Ci.ffIConverterItem);
-
- //loop through the properties
- for (var property in aItem)
- item.setProperty(property, aItem[property]);
-
- //set the description
- var description = item.name;
- try {
- description = this.bundle.GetStringFromName("ff.converters." +
- item.conversion + "." +
- item.name);
- } finally {
- item.setProperty("description", description);
- }
-
- //set the ID
- var id = item.conversion + "-" + String(item.units) + "-" + item.name;
- item.setProperty("ID", id);
-
- //return the item
- return item;
- },
-
- /**
- * Load the converter data.
- *
- * @return False if the load fails.
- */
- _loadConverters: function ConverterService__loadConverters()
- {
- //setup error variables
- const PREFIX = "ff.converters.load.";
- var name = this.bundle.GetStringFromName(PREFIX + "name");
- var message = "";
-
- //get the disk service
- var mgrSvc = Cc["@ensolis.com/forecastfox/manager-service;1"].
- getService(Ci.ffIManagerService);
- var dskSvc = mgrSvc.disk;
-
- //get the conversion file
- var file = dskSvc.get("converters.js", TYPE_DEFAULTS);
-
- //file doesn't exist
- if (!file.exists()) {
- message = this.bundle.GetStringFromName(PREFIX + "exists.message");
- this._error.init(SEVERITY_ERROR, name, message);
- return false;
- }
-
- //file is not readable
- if (!file.isReadable()) {
- message = this.bundle.formatStringFromName(PREFIX + "read.message",
- [this._file.path], 1);
- this._error.init(SEVERITY_ERROR, name, message);
- return false;
- }
-
- //get the content of the file
- var content = dskSvc.readText(file);
- if (!content) {
- message = this.bundle.GetStringFromName(PREFIX + "empty.message");
- this._error.init(SEVERITY_ERROR, name, message);
- return false;
- }
-
- //save the data in the items variable
- try {
- this._items = eval(content);
- } catch(e) {
- this._items = {};
- this._error.init(SEVERITY_ERROR, name, e.message);
- return false;
- }
-
- //loaded successfully
- return true;
- }
- };